package a;

import t1.IAdd;

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

public class A {
    public static void main(String[] args) throws Exception {
//        System.out.println(System.class.getClassLoader());
//        System.out.println("the Launcher's classloader is " + sun.misc.Launcher.getLauncher().getClass().getClassLoader());

        URLClassLoader  classloader = (URLClassLoader)ClassLoader.getSystemClassLoader();
        //file:/e:/code/workspace/testJar/out/Add.jar
        String url = "file:/F:/workspace/ideawp/hotLoadClassDemon/target/hotLoadClass-1.0-SNAPSHOT.jar";
        URL classUrl = new URL(url);

        Method addURL = URLClassLoader.class.getDeclaredMethod("addURL",new Class[] { URL.class });
        addURL.setAccessible(true);
        addURL.invoke(classloader, new Object[] { classUrl });

        //String className = "t1.Add";
        String className = "t1.Add";
        Class<?> c = Class.forName(className);
        System.out.println("loadclass :"+c);
        IAdd add = (IAdd) c.newInstance();
        System.out.println(add.add(1,2));

        Thread.sleep(20*1000);

        Method addURL2 = URLClassLoader.class.getDeclaredMethod("addURL",new Class[] { URL.class });
        addURL2.setAccessible(true);
        addURL2.invoke(classloader, new Object[] { classUrl });


        Class<?> c2 = Class.forName(className);
        System.out.println("loadclass :"+c2);
        IAdd add2 = (IAdd) c2.newInstance();
        System.out.println(add2.add(1,2));

    }
}
